package org.bringer.webapp.pages;


import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.tapestry5.PropertyConduit;
import org.apache.tapestry5.alerts.AlertManager;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.beaneditor.PropertyModel;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;
import org.bringer.dao.SearchException;
import org.bringer.model.Account;
import org.bringer.model.Annotation;
import org.bringer.service.AnnotationManager;
import org.bringer.webapp.services.SecurityContext;
import org.slf4j.Logger;


/**
 * Main entry point of the application
 *
 * @author Serge Eby
 * @version $Id: Home.java 5 2008-08-30 09:59:21Z serge.eby $
 */
public class Annotations {
	@Property
    private String errorMessage;
    @Inject
    private Logger logger;

    @Inject
    private Messages messages;

    @Inject
    private SecurityContext securityContext;
    
    @Inject
    private BeanModelSource beanModelSource;

    @Inject
    private AlertManager alertManager;

    @InjectPage
    private AnnotationEdit annotationEdit;
    
    @Inject
    private AnnotationManager annotationManager;

    @Persist
    @Property
    private Annotation current;
   


	@Property
    private List<Annotation> annotationList;
   
  
	public BeanModel<Annotation> getModel() {
        final BeanModel<Annotation> model = beanModelSource.createDisplayModel(Annotation.class, messages);
        model.include("description", "amount", "annotationDate");
        model.get("description").label(messages.get("annotation.description"));
        
        model.get("amount").label(messages.get("annotation.amount"));
        model.get("annotationDate").label(messages.get("annotation.date"));
        
        final PropertyModel pm1 = model.add("account", 
        		createAccountPropertyConduit()).label(messages.get("annotation.account"));
        pm1.sortable(true);
	        
        // Set labels
     

        return model;
    }
   
	private PropertyConduit createAccountPropertyConduit() {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		return new PropertyConduit() {
			
			@Override
			public <T extends java.lang.annotation.Annotation> T getAnnotation(Class<T> arg0) {
				// TODO Auto-generated method stub
				return null;
			}
			
			@Override
			public void set(Object instance, Object value) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public Class getPropertyType() {
				// TODO Auto-generated method stub
				return Double.class;
			}
			
			@Override
			public Object get(Object instance) {
				Annotation ac=(Annotation)instance;
				String iban="";
				if (ac.getAccount()!=null){
					iban=ac.getAccount().getIbanCode();
				}
				
				return iban;
			}
		};
	}

	void setupRender() {
        try {
        	annotationList = annotationManager.getAnnotationsByAccount("123");
        } catch (SearchException se) {
            errorMessage = se.getMessage();           
        }
        logger.debug("after search.. retrieved "+annotationList.size());
    }
    
    
  /**
	
    Object onActionFromEdit(Annotation currentAnnotation) {
        //logger.debug("fetching account with iban: " + currentAccount);
        
        //return annotationEdit.initialize(currentAccount, "list");
    	return null;
    }
    **/
	public String getBirthDate() {
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		return format.format(current.getAnnotationDate().getTime());
	} 
	public String getAssociatedAccount() {
		
		return current.getAccount().getIbanCode();
	}
	Object onAdd() {
        return annotationEdit.initialize(null, "list");
 }
	Object onActionFromEdit(Annotation currentAnnotation) {
        logger.debug("fetching annotation with id: " + currentAnnotation.getId());
        
        return annotationEdit.initialize(currentAnnotation, "list");
    }
}
